#include    "texture2d.h"
#include    "../common.h"
#include    "../../../external/soil/include/soil.h"


using namespace PersonalTexture;

GLuint Texture2D::LoadTexture(const char* fileName)
{
    GLuint texture = 0;
    texture = SOIL_load_OGL_texture(fileName, SOIL_LOAD_AUTO,SOIL_CREATE_NEW_ID, 0);

    if(texture == 0)
    {
       PERSONAL_DEBUG_LOG("load file failed!");
    }
    return texture;
}

bool Texture2D::ChangeProportion(Proportion& proportion,const Texture2DSize* textureSize,const TextureArea* showArea)
{
    if(textureSize == 0)
    {
        PERSONAL_DEBUG_LOG("Texture2D::DrawTexture textureSize is null!");
        return false;
    }

    proportion.x = 0;
    proportion.y = 0;
    proportion.width = 1.0f;
    proportion.height = 1.0f;

    if(showArea != 0)
    {
         proportion.x = showArea->x / textureSize->width;
         proportion.y = showArea->y / textureSize->height;

         proportion.width   = showArea->width / textureSize->width;
         proportion.height  = showArea->height / textureSize->height;
    }

    return true;
}

bool Texture2D::CheckParameter(const GLuint textureID,const Point* position,const Texture2DSize* textureSize)
{
     if(textureID == 0)
    {
        PERSONAL_DEBUG_LOG("Texture2D::DrawTexture textureid is null!");
        return false;
    }

    if(position == 0)
    {
        PERSONAL_DEBUG_LOG("Texture2D::DrawTexture position is null!");
        return false;
    }

    if(textureSize == 0)
    {
        PERSONAL_DEBUG_LOG("Texture2D::DrawTexture textureSize is null!");
        return false;
    }

    return true;
}


void Texture2D::DrawTexture(const GLuint textureID,const Point* position,const Texture2DSize* textureSize,const TextureArea* showArea)
{
    if(false == Texture2D::CheckParameter(textureID,position,textureSize))
    {
        return;
    }

    Proportion portion;
    if( false == Texture2D::ChangeProportion(portion,textureSize,showArea))
    {
        return;
    }

    GLfloat w = textureSize->width;
    GLfloat h = textureSize->height;
    if(showArea)
    {
        w = showArea->width;
        h = showArea->height;
    }


    glBindTexture(GL_TEXTURE_2D,textureID);
    glBegin(GL_QUADS);


    glTexCoord2f(portion.x,portion.y);                                      glVertex2f(position->x,position->y);
    glTexCoord2f(portion.x + portion.width,portion.y );                     glVertex2f(position->x + w, position->y);
    glTexCoord2f(portion.x + portion.width,portion.y + portion.height);     glVertex2f(position->x + w,position->y + h);
    glTexCoord2f(portion.x , portion.y + portion.height);                   glVertex2f(position->x , position->y + h);


    glEnd();
}

void Texture2D::DrawReverseTexture(const GLuint textureID,const Point* position,const Texture2DSize* textureSize,const TextureArea* showArea)
{
    if(false == Texture2D::CheckParameter(textureID,position,textureSize))
    {
        return;
    }

    Proportion portion;
    if( false == Texture2D::ChangeProportion(portion,textureSize,showArea))
    {
        return;
    }

    GLfloat w = textureSize->width;
    GLfloat h = textureSize->height;
    if(showArea)
    {
        w = showArea->width;
        h = showArea->height;
    }

    glBindTexture(GL_TEXTURE_2D,textureID);
    glBegin(GL_QUADS);


    glTexCoord2f(portion.x + portion.width,portion.y );                     glVertex2f(position->x,position->y);
    glTexCoord2f(portion.x,portion.y);                                      glVertex2f(position->x + w, position->y);
    glTexCoord2f(portion.x , portion.y + portion.height);                   glVertex2f(position->x + w,position->y + h);
    glTexCoord2f(portion.x + portion.width,portion.y + portion.height);     glVertex2f(position->x , position->y + h);


    glEnd();
}
